package com.tskj.mh.studyroom.controller;

import com.alibaba.fastjson.JSONObject;
import com.tskj.mh.studyroom.service.StudentService;
import com.tskj.mh.studyroom.service.StudyRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 自习室预约服务控制层
 * @author LeonSu
 */
@Controller
@RequestMapping("/studyroom")
public class StudyRoomController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private StudyRoomService studyRoomService;

    @GetMapping("/list")
    @ResponseBody
    public JSONObject getStudyRoomList() {
        JSONObject ret = new JSONObject();
        ret.put("code", 0);
        ret.put("data", studyRoomService.getStudyRoomList());
        return ret;
    }

    @GetMapping("/timeList")
    @ResponseBody
    public JSONObject getTimeList(@RequestParam String token, @RequestParam String roomId, @RequestParam String date) {
        String adUserName = studentService.getAdUserName(token);
        JSONObject ret = new JSONObject();
        ret.put("code", 0);
        ret.put("data", studyRoomService.getTimeList(adUserName, roomId, date));
        return ret;
    }

    @GetMapping("/checkStopTime")
    @ResponseBody
    public JSONObject checkStopTime(@RequestParam String token) {
        String adUserName = studentService.getAdUserName(token);
        JSONObject ret = new JSONObject();
        ret.put("code", 0);
        ret.put("data", studyRoomService.checkStopTime(adUserName));
        return ret;
    }

    /**
     * 保存自习室订单
     * @param data
     * @return
     */
    @PostMapping("/saveStudyRoomOrder")
    @ResponseBody
    public JSONObject saveOrder(@RequestParam String token, @RequestBody JSONObject data) {
        String adUserName = studentService.getAdUserName(token);
        JSONObject student = studentService.getStudentInfo(adUserName);
        return studyRoomService.saveOrder(student, data);
    }
}
